import wx
import abc
import ModelRecord
import SimplePanels
from wx.lib.pubsub import setuparg1
from wx.lib.pubsub import pub


import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure

class ViewBaseChart(wx.Panel):
    __metaclass__ = abc.ABCMeta
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.init_widget()
        self.init_control()
        ret = self.get_data()
        self.do_draw(ret)
        self.canvas.draw()

    def init_widget(self):
        """
        1. canvas figure, matplotlib stuff
        """
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
        
    def init_control(self):
        #subscribe on_sync_done
        pub.subscribe(self.on_sync_done, ModelRecord.ModelRecord.M_REFRESH)

    def on_sync_done(self, m):
        #call do_draw to do it all again
        self.axes.clear()
        ret = self.get_data()
        self.do_draw(ret)
        self.canvas.draw()

    @abc.abstractmethod
    def do_draw(self):
        """
        pie or trend
        """
        #tm = self.GetParent().time_mode
        #ref = self.GetParent().time_ref
        #print(tm,ref)
        pass
    
    @abc.abstractmethod
    def get_data(self):
        """
        pie or trend
        """
        #tm = self.GetParent().time_mode
        #ref = self.GetParent().time_ref
        #print(tm,ref)
        pass
